<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Template extends Admin_Controller
{
	function checkbox()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_checkbox_model');
		
		// add to view
		$this->add('name', $CI->template_checkbox_model->name);
		$this->add('values', $CI->template_checkbox_model->values);
		$this->add('items', $CI->template_checkbox_model->items);
		
		// view
		$this->view('template_checkbox_view');
	}
	
	/**
	* called in module
	*/
	function dropdown()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_dropdown_model');
		
		// add to view
		$this->add('name', $CI->template_dropdown_model->name);
		$this->add('onchange', $CI->template_dropdown_model->onchange);
		$this->add('value', $CI->template_dropdown_model->value);
		$this->add('items', $CI->template_dropdown_model->items);
		
		// view
		$this->view('template_dropdown_view');
	}
	
	function show_template()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_detail_model');
		
		$id = $CI->uri->segment(3);
		$q = $CI->db->get_where(TEMPLATES, array(TEMPLATES_FRANCHISE_ID=>$CI->template_detail_model->admin_id(), TEMPLATES_TEMPLATE_TYPE_ID=>$id));
		$row = $q->row();
		
		// add to view
		$this->add('subject', $row?$row->{TEMPLATES_SUBJECT}:'');
		$this->add('body', $row?$row->{TEMPLATES_BODY}:'');		
		
		// view
		$this->use_global_template(FALSE);
		$this->view('show_template_view');
	}
		
	function lists()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_list_model');
		
		// filter
		if (isset($_POST['filter']))
			$CI->template_list_model->cache->add($CI->template_list_model->filter_cache_prefix, $_POST['filter']);

		// sql
		$CI->db->where(TEMPLATES.'.'.TEMPLATES_FRANCHISE_ID, $CI->template_list_model->admin_id());
		$q = $CI->db->get(TEMPLATES);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->template_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->template_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->from(TEMPLATES);
		$CI->db->select(TEMPLATES.'.'.TEMPLATES_TEMPLATE_TYPE_ID.','.TEMPLATES.'.'.TEMPLATES_SUBJECT.','.TEMPLATE_TYPES.'.'.TEMPLATE_TYPES_NAME);
		$CI->db->join(TEMPLATE_TYPES, TEMPLATE_TYPES.'.'.TEMPLATE_TYPES_TEMPLATE_TYPE_ID.'='.TEMPLATES.'.'.TEMPLATES_TEMPLATE_TYPE_ID);
		$CI->db->where(TEMPLATES.'.'.TEMPLATES_FRANCHISE_ID, $CI->template_list_model->admin_id());
		foreach ($CI->template_list_model->columns as $k=>$v)
		{
			if ($CI->template_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->template_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->template_list_model->start);
		$q = $CI->db->get();
		
		// gridview
		$this->add('items', $q->result());
		$this->add('start', $CI->template_list_model->start);
		foreach ($CI->template_list_model->columns as $k=>$v)
		{
			if ($CI->template_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->template_list_model->cache->load($k).'"');
			else
				$this->add('sort_'.$k,  '');
		}

		// message
		$this->add('msg', $CI->template_list_model->cache->load($CI->template_list_model->msg_cache_prefix));
		$CI->template_list_model->cache->add($CI->template_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('template.toolbar', array('parent_id'=>0, 'start'=>$CI->template_list_model->start));
		$this->module_navigation('Email Template Management ');
		
		// view
		$this->view('template_list_view');
	}
	
	function detail()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_detail_model');
		
		// sql	
		$q = $CI->db->get_where(TEMPLATES, array(TEMPLATES_FRANCHISE_ID=>$CI->template_detail_model->admin_id(), TEMPLATES_TEMPLATE_TYPE_ID=>$CI->template_detail_model->item));
		$entity = $q->row();

		// add to view
		$this->add('subject', $CI->template_detail_model->cache->load('msg')?$CI->template_detail_model->cache->load('subject'):($entity?$entity->{TEMPLATES_SUBJECT}:''));
		$CI->template_detail_model->cache->add('subject', '');
		
		// template type dropdown
		$CI->load->module_model('template_type.template_type_dropdown_model');
		$CI->template_type_dropdown_model->name = 'template_type_id';
		$CI->template_type_dropdown_model->value = $CI->template_detail_model->cache->load('msg')?$CI->template_detail_model->cache->load('template_type_id'):($entity?$entity->{TEMPLATES_TEMPLATE_TYPE_ID}:'');
		$CI->template_detail_model->cache->add('template_type_id', '');
		
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'body';
		$CI->fckeditor->Height = 500;
		$CI->fckeditor->Value = $CI->template_detail_model->cache->load('msg')?$CI->template_detail_model->cache->load('body'):($entity?$entity->{TEMPLATES_BODY}:'');
		$CI->template_detail_model->cache->add('body', '');
		$CI->fckeditor->SetPathOfFranchise();
		$this->add('body', $CI->fckeditor->CreateHtml());
		
		// add to view
		$this->add('start', $CI->template_detail_model->start);
		$this->add('item_id', $CI->template_detail_model->item);

		// message
		$this->add('msg', $CI->template_detail_model->cache->load($CI->template_detail_model->msg_cache_prefix));
		$CI->template_detail_model->cache->add($CI->template_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->template_detail_model->item)
			$this->module_navigation('Email Template : Edit');
		else
			$this->module_navigation('Email Template : New');
			
		// view
		$this->view('template_detail_view');
	}
}

?>